/*   
 * Copyright 2011-2012 the original author or authors.   
 *   
 * Licensed under the Apache License, Version 2.0 (the "License");   
 * you may not use this file except in compliance with the License.   
 * You may obtain a copy of the License at   
 *   
 *      http://www.apache.org/licenses/LICENSE-2.0   
 *   
 * Unless required by applicable law or agreed to in writing, software   
 * distributed under the License is distributed on an "AS IS" BASIS,   
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   
 * See the License for the specific language governing permissions and   
 * limitations under the License.   
 */
package code.google.com.p.antigovframe.sample

import javax.persistence.*

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
 
import code.google.com.p.antigovframe.sample.domain.Task;
/**
 * Data Transfer Object
 * 
 * @author Jacob Lee
 * @since 2012. 7. 20.
 * 
 */
class BaseDTOTest<T> extends GroovyTestCase {
	protected static Validator validator; 
	
	def violationMap=[:];

	void setUp() {
		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		validator = factory.getValidator();
	}
	
	boolean isValid(T t) {
		Set<ConstraintViolation<T>> violations =  validate(t);
//		for (ConstraintViolation<T> violation : violations) {
//		 // LOGGER.debug(violation.toString());
//		 //println violation.toString(); 
//		}
		return (violations.size() == 0);
	  }
	
	 private Set<ConstraintViolation<T>> validate(T t) {
		return validator.validate(t);
	  }
	 
	Object checkValidated(T t){
		 
		 Set<ConstraintViolation<T>>  violations =  validate(t);
		 
		 violations.each { 
			 if (violationMap.containsKey(it.propertyPath) ) {
				 def list= violationMap.get(it.propertyPath) 
				  list +="$it.interpolatedMessage";
			 }
			 else {
				 def list=[ "$it.interpolatedMessage"] ;
				 violationMap.put(it.propertyPath.toString(), "$it.interpolatedMessage");
			 }
			// violationMap +="$it.propertyPath $it.interpolatedMessage $it.messageTemplate";
		 }
		return violationMap;
	 }
	
	/**
	 * Is the Property in the Violations map ? 
	 * 
	 * @param property
	 * @param violations
	 */
	void mustViolating(String property) {
		switch (property) {
			case violationMap: "$property in violations map"; break
			default: throw new Exception("$property is not in violations map")
		}
	}
	/**
	 * Test a mandatory property 
	 *
	 * @param property
	 * @param violations
	 */
	void mustNullViolating(String property) {
		switch (property) {
			case violationMap: 
				//println violationMap.get(property)
				"$property in violations map"; break
			default: throw new Exception("$property is not in violations map")
		}
	}
	/**
	 * Test a regular expression for the model property
	 *
	 * @param property
	 * @param violations
	 */
	void mustMatchPattern(String property) {
		switch (property) {
			case violationMap:
				//println violationMap.get(property) ;
			   // "$property in violations map"
				break
			default: throw new Exception("$property is not in violations map")
		}
	}
	
	void testDefaultConstraints() {
		
	}
}
